#! /usr/bin/perl

use Net::SMTP;

sub get_today
{ 
	my($sec, $min, $hour, $day, $month, $year) = localtime(time());
	$year += 1900;
	$month++;
	my $today = sprintf("%04d.%02d.%02d", $year, $month, $day);
	return $today;
}

# mail_user should be your_mail@hbgk.net
sub send_mail
{
	my $mail_user = "TracBackup";
	my $mail_rcpt = "xwli\@hbgk.net";
	my $mail_cc = "xwli\@hbgk.net";
	my $mail_server = "192.168.0.1";
	my $mail_subject = "Subject: Trac Backup Check Results\n";

	open(SESAME, "./report") || die "can not open ./report";
	my @mail_content = <SESAME>;
	close(SESAME);

	my $smtp = Net::SMTP->new($mail_server);

	$smtp->mail($mail_user);
	$smtp->to($mail_rcpt);
	$smtp->cc($mail_cc);

	$smtp->data();
	$smtp->datasend("From: $mail_user\n");
	$smtp->datasend("To: $mail_rcpt\n");
	$smtp->datasend("Cc: $mail_cc\n");
	$smtp->datasend($mail_subject);
	$smtp->datasend("\n");
	foreach my $tmp_content (@mail_content)
	{
		print $tmp_content;
		$smtp->datasend($tmp_content);
	}
	$smtp->dataend();

	$smtp->quit();
}

my @mysql_database = ("trac");
my $bak_dir = "/home/trac/trac_backup/data/";
my $title_cmd = "echo \"The following is the contrast mysqldump result:\n\" >./report";

system $title_cmd || die "exec failed/n";

foreach my $tmp_database(@mysql_database)
{
	my $tmp_bak_dir	= $bak_dir.$tmp_database.".".&get_today.".sql";
	my $result_cmd = "";

	print $tmp_bak_dir."\n";
	if (-e $tmp_bak_dir)
	{
		$result_cmd = "echo \"Backup database $tmp_database success\n\" >>./report";
	}
	else
	{
		$result_cmd = "echo \"Backup database $tmp_database failed\n\" >>./report";
	}
	system $result_cmd || die "exec failed/n";
}

&send_mail;
